﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using quanlycd3lop.GUI;
using quanlycd3lop.Data;

namespace quanlycd3lop.BL
{
    class cl_KhachHang
    {AccessData con;
        Form frm;
        DataTable tb;
        public cl_KhachHang(frm_KhachHang f)
        {
            con = new AccessData();
            frm = f;
        }
        public void refesh()
        {
            ((DataGridView)frm.Controls["DataGridView1"]).DataSource = tb;
            ((ComboBox)frm.Controls["cbx_Makh"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_Tenkh"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_Sodt"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_Diachi"]).DataBindings.Clear();

            ((ComboBox)frm.Controls["cbx_Makh"]).DataBindings.Add("Text",tb,"MaKH");
            ((TextBox)frm.Controls["txt_Tenkh"]).DataBindings.Add("Text", tb, "TenKH");
            ((TextBox)frm.Controls["txt_SoDT"]).DataBindings.Add("Text", tb, "SoDT");
            ((TextBox)frm.Controls["txt_Diachi"]).DataBindings.Add("Text", tb, "DiaChi");
            
        }
        public void hienthi(string MaKH)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            if (MaKH != null)
            {
                pr[0] = new SqlParameter("@MaKH", MaKH);
            }
            else
            {
                pr[0] = new SqlParameter("@MaKH", DBNull.Value);
            }
            tb = con.Excute_ps("ps_laykhachhang",pr);
            con.Close();
            refesh();
        }
        public void themmoi()
        {
            string str_MaKH, str_SoDT, str_TenKH, str_DiaChi;
            str_MaKH = ((ComboBox)frm.Controls["cbx_Makh"]).Text;
            str_TenKH = ((TextBox)frm.Controls["txt_Tenkh"]).Text;
            str_SoDT = ((TextBox)frm.Controls["txt_SoDT"]).Text;
            str_DiaChi = ((TextBox)frm.Controls["txt_Diachi"]).Text;

            SqlParameter[] pr = { new SqlParameter("@MaKH",str_MaKH),
                                  new SqlParameter("@Tenkh",str_TenKH),
                                  new SqlParameter("@SoDT",str_SoDT),
                                  new SqlParameter("@DiaChi",str_DiaChi)
                                };
            con.Open();
            tb = con.Excute_ps("ps_themkhachhang",pr);
            con.Close();
            refesh();
        }
        public void xoa()
        {
            string str_MaKH;
            str_MaKH = ((ComboBox)frm.Controls["cbx_Makh"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaKH",str_MaKH)
                                };
            con.Open();
            tb = con.Excute_ps("ps_xoakhachhang", pr);
            con.Close();
            refesh();
        }
        public void sua()
        {
            string str_MaKH,str_TenKH, str_SoDT,str_DiaChi;
            str_MaKH = ((ComboBox)frm.Controls["cbx_Makh"]).Text;
            str_TenKH = ((TextBox)frm.Controls["txt_Tenkh"]).Text;
            str_SoDT = ((TextBox)frm.Controls["txt_SoDT"]).Text;
            str_DiaChi = ((TextBox)frm.Controls["txt_Diachi"]).Text;

            SqlParameter[] pr = { new SqlParameter("@MaKH",str_MaKH),
                                  new SqlParameter("@TenKH",str_TenKH),
                                  new SqlParameter("@SoDT",str_SoDT),
                                  new SqlParameter("@DiaChi",str_DiaChi)
                                };
            con.Open();
            tb = con.Excute_ps("ps_suakhachhang", pr);
            con.Close();
            refesh();
        }
        public void set_combobox1()
           {
                con.Open();
                SqlParameter[] pr = new SqlParameter[1];
                pr[0] = new SqlParameter("@MaKH",DBNull.Value);
                DataTable db = con.Excute_ps("ps_laykhachhang", pr);
                ComboBox cb = ((ComboBox)frm.Controls["cbx_Makh"]);
                cb.DataSource = db;
                cb.DisplayMember = "MaKH";
                cb.ValueMember = "MaKH";
           }
    }
}
